

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.nn.layer.combined &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.nn.layer.combined</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.nn.layer.combined</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020-2021 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>
<span class="sd">&quot;&quot;&quot;Combined cells.&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">nn</span>
<span class="kn">from</span> <span class="nn">mindspore.ops.primitive</span> <span class="kn">import</span> <span class="n">Primitive</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Validator</span>
<span class="kn">from</span> <span class="nn">.normalization</span> <span class="kn">import</span> <span class="n">BatchNorm2d</span><span class="p">,</span> <span class="n">BatchNorm1d</span>
<span class="kn">from</span> <span class="nn">.activation</span> <span class="kn">import</span> <span class="n">get_activation</span><span class="p">,</span> <span class="n">LeakyReLU</span>
<span class="kn">from</span> <span class="nn">..cell</span> <span class="kn">import</span> <span class="n">Cell</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;Conv2dBnAct&#39;</span><span class="p">,</span>
    <span class="s1">&#39;DenseBnAct&#39;</span>
<span class="p">]</span>


<div class="viewcode-block" id="Conv2dBnAct"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.Conv2dBnAct.html#mindspore.nn.Conv2dBnAct">[docs]</a><span class="k">class</span> <span class="nc">Conv2dBnAct</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A combination of convolution, Batchnorm, and activation layer.</span>

<span class="sd">    This part is a more detailed overview of Conv2d operation.</span>

<span class="sd">    Args:</span>
<span class="sd">        in_channels (int): The number of input channel :math:`C_{in}`.</span>
<span class="sd">        out_channels (int): The number of output channel :math:`C_{out}`.</span>
<span class="sd">        kernel_size (Union[int, tuple]): The data type is int or a tuple of 2 integers. Specifies the height</span>
<span class="sd">            and width of the 2D convolution window. Single int means the value is for both height and width of</span>
<span class="sd">            the kernel. A tuple of 2 ints means the first value is for the height and the other is for the</span>
<span class="sd">            width of the kernel.</span>
<span class="sd">        stride (int): Specifies stride for all spatial dimensions with the same value. The value of stride must be</span>
<span class="sd">            greater than or equal to 1 and lower than any one of the height and width of the `x`. Default: 1.</span>
<span class="sd">        pad_mode (str): Specifies padding mode. The optional values are &quot;same&quot;, &quot;valid&quot;, &quot;pad&quot;. Default: &quot;same&quot;.</span>
<span class="sd">        padding (int): Implicit paddings on both sides of the `x`. Default: 0.</span>
<span class="sd">        dilation (int): Specifies the dilation rate to use for dilated convolution. If set to be :math:`k &gt; 1`,</span>
<span class="sd">            there will be :math:`k - 1` pixels skipped for each sampling location. Its value must be greater than</span>
<span class="sd">            or equal to 1 and lower than any one of the height and width of the `x`. Default: 1.</span>
<span class="sd">        group (int): Splits filter into groups, `in_ channels` and `out_channels` must be</span>
<span class="sd">            divisible by the number of groups. Default: 1.</span>
<span class="sd">        has_bias (bool): Specifies whether the layer uses a bias vector. Default: False.</span>
<span class="sd">        weight_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the convolution kernel.</span>
<span class="sd">            It can be a Tensor, a string, an Initializer or a number. When a string is specified,</span>
<span class="sd">            values from &#39;TruncatedNormal&#39;, &#39;Normal&#39;, &#39;Uniform&#39;, &#39;HeUniform&#39; and &#39;XavierUniform&#39; distributions as well</span>
<span class="sd">            as constant &#39;One&#39; and &#39;Zero&#39; distributions are possible. Alias &#39;xavier_uniform&#39;, &#39;he_uniform&#39;, &#39;ones&#39;</span>
<span class="sd">            and &#39;zeros&#39; are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of</span>
<span class="sd">            Initializer for more details. Default: &#39;normal&#39;.</span>
<span class="sd">        bias_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the bias vector. Possible</span>
<span class="sd">            Initializer and string are the same as &#39;weight_init&#39;. Refer to the values of</span>
<span class="sd">            Initializer for more details. Default: &#39;zeros&#39;.</span>
<span class="sd">        has_bn (bool): Specifies to used batchnorm or not. Default: False.</span>
<span class="sd">        momentum (float): Momentum for moving average for batchnorm, must be [0, 1]. Default:0.997</span>
<span class="sd">        eps (float): Term added to the denominator to improve numerical stability for batchnorm, should be greater</span>
<span class="sd">            than 0. Default: 1e-5.</span>
<span class="sd">        activation (Union[str, Cell, Primitive]): Specifies activation type. The optional values are as following:</span>
<span class="sd">            &#39;softmax&#39;, &#39;logsoftmax&#39;, &#39;relu&#39;, &#39;relu6&#39;, &#39;tanh&#39;, &#39;gelu&#39;, &#39;sigmoid&#39;,</span>
<span class="sd">            &#39;prelu&#39;, &#39;leakyrelu&#39;, &#39;hswish&#39;, &#39;hsigmoid&#39;. Default: None.</span>
<span class="sd">        alpha (float): Slope of the activation function at x &lt; 0 for LeakyReLU. Default: 0.2.</span>
<span class="sd">        after_fake(bool): Determine whether there must be a fake quantization operation after Cond2dBnAct.</span>
<span class="sd">            Default: True.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **x** (Tensor) - Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`. The data type is float32.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor of shape :math:`(N, C_{out}, H_{out}, W_{out})`. The data type is float32.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `in_channels`, `out_channels`, `stride`, `padding` or `dilation` is not an int.</span>
<span class="sd">        TypeError: If `has_bias` is not a bool.</span>
<span class="sd">        ValueError: If `in_channels` or `out_channels` `stride`, `padding` or `dilation` is less than 1.</span>
<span class="sd">        ValueError: If `pad_mode` is not one of &#39;same&#39;, &#39;valid&#39;, &#39;pad&#39;.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; net = nn.Conv2dBnAct(120, 240, 4, has_bn=True, activation=&#39;relu&#39;)</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.ones([1, 120, 1024, 640]), mindspore.float32)</span>
<span class="sd">        &gt;&gt;&gt; result = net(x)</span>
<span class="sd">        &gt;&gt;&gt; output = result.shape</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        (1, 240, 1024, 640)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">in_channels</span><span class="p">,</span>
                 <span class="n">out_channels</span><span class="p">,</span>
                 <span class="n">kernel_size</span><span class="p">,</span>
                 <span class="n">stride</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
                 <span class="n">pad_mode</span><span class="o">=</span><span class="s1">&#39;same&#39;</span><span class="p">,</span>
                 <span class="n">padding</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                 <span class="n">dilation</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
                 <span class="n">group</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
                 <span class="n">has_bias</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">weight_init</span><span class="o">=</span><span class="s1">&#39;normal&#39;</span><span class="p">,</span>
                 <span class="n">bias_init</span><span class="o">=</span><span class="s1">&#39;zeros&#39;</span><span class="p">,</span>
                 <span class="n">has_bn</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">momentum</span><span class="o">=</span><span class="mf">0.997</span><span class="p">,</span>
                 <span class="n">eps</span><span class="o">=</span><span class="mf">1e-5</span><span class="p">,</span>
                 <span class="n">activation</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">alpha</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span>
                 <span class="n">after_fake</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize Conv2dBnAct.&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Conv2dBnAct</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">conv</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">(</span><span class="n">in_channels</span><span class="p">,</span>
                              <span class="n">out_channels</span><span class="p">,</span>
                              <span class="n">kernel_size</span><span class="o">=</span><span class="n">kernel_size</span><span class="p">,</span>
                              <span class="n">stride</span><span class="o">=</span><span class="n">stride</span><span class="p">,</span>
                              <span class="n">pad_mode</span><span class="o">=</span><span class="n">pad_mode</span><span class="p">,</span>
                              <span class="n">padding</span><span class="o">=</span><span class="n">padding</span><span class="p">,</span>
                              <span class="n">dilation</span><span class="o">=</span><span class="n">dilation</span><span class="p">,</span>
                              <span class="n">group</span><span class="o">=</span><span class="n">group</span><span class="p">,</span>
                              <span class="n">has_bias</span><span class="o">=</span><span class="n">has_bias</span><span class="p">,</span>
                              <span class="n">weight_init</span><span class="o">=</span><span class="n">weight_init</span><span class="p">,</span>
                              <span class="n">bias_init</span><span class="o">=</span><span class="n">bias_init</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">has_bn</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">has_bn</span><span class="p">,</span> <span class="s2">&quot;has_bn&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">has_act</span> <span class="o">=</span> <span class="n">activation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">after_fake</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">after_fake</span><span class="p">,</span> <span class="s2">&quot;after_fake&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">has_bn</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">batchnorm</span> <span class="o">=</span> <span class="n">BatchNorm2d</span><span class="p">(</span><span class="n">out_channels</span><span class="p">,</span> <span class="n">eps</span><span class="p">,</span> <span class="n">momentum</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">activation</span> <span class="o">==</span> <span class="s2">&quot;leakyrelu&quot;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">activation</span> <span class="o">=</span> <span class="n">LeakyReLU</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">activation</span> <span class="o">=</span> <span class="n">get_activation</span><span class="p">(</span><span class="n">activation</span><span class="p">)</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">activation</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="k">else</span> <span class="n">activation</span>
            <span class="k">if</span> <span class="n">activation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">,</span> <span class="p">(</span><span class="n">Cell</span><span class="p">,</span> <span class="n">Primitive</span><span class="p">)):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For &#39;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="si">}</span><span class="s2">&#39;, the &#39;activation&#39; must be str or Cell or Primitive, &quot;</span>
                                <span class="sa">f</span><span class="s2">&quot;but got </span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="n">activation</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2">.&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">conv</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_bn</span><span class="p">:</span>
            <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">batchnorm</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_act</span><span class="p">:</span>
            <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">x</span></div>


<div class="viewcode-block" id="DenseBnAct"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.DenseBnAct.html#mindspore.nn.DenseBnAct">[docs]</a><span class="k">class</span> <span class="nc">DenseBnAct</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A combination of Dense, Batchnorm, and the activation layer.</span>

<span class="sd">    This part is a more detailed overview of Dense op.</span>

<span class="sd">    Args:</span>
<span class="sd">        in_channels (int): The number of channels in the input space.</span>
<span class="sd">        out_channels (int): The number of channels in the output space.</span>
<span class="sd">        weight_init (Union[Tensor, str, Initializer, numbers.Number]): The trainable weight_init parameter. The dtype</span>
<span class="sd">            is same as `x`. The values of str refer to the function `initializer`. Default: &#39;normal&#39;.</span>
<span class="sd">        bias_init (Union[Tensor, str, Initializer, numbers.Number]): The trainable bias_init parameter. The dtype is</span>
<span class="sd">            same as `x`. The values of str refer to the function `initializer`. Default: &#39;zeros&#39;.</span>
<span class="sd">        has_bias (bool): Specifies whether the layer uses a bias vector. Default: True.</span>
<span class="sd">        has_bn (bool): Specifies to use batchnorm or not. Default: False.</span>
<span class="sd">        momentum (float): Momentum for moving average for batchnorm, must be [0, 1]. Default:0.9</span>
<span class="sd">        eps (float): Term added to the denominator to improve numerical stability for batchnorm, should be greater</span>
<span class="sd">            than 0. Default: 1e-5.</span>
<span class="sd">        activation (Union[str, Cell, Primitive]): Specifies activation type. The optional values are as following:</span>
<span class="sd">            &#39;softmax&#39;, &#39;logsoftmax&#39;, &#39;relu&#39;, &#39;relu6&#39;, &#39;tanh&#39;, &#39;gelu&#39;, &#39;sigmoid&#39;,</span>
<span class="sd">            &#39;prelu&#39;, &#39;leakyrelu&#39;, &#39;hswish&#39;, &#39;hsigmoid&#39;. Default: None.</span>
<span class="sd">        alpha (float): Slope of the activation function at x &lt; 0 for LeakyReLU. Default: 0.2.</span>
<span class="sd">        after_fake(bool): Determine whether there must be a fake quantization operation after DenseBnAct.</span>
<span class="sd">            Default: True.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **x** (Tensor) - Tensor of shape :math:`(N, in\_channels)`. The data type is float32.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor of shape :math:`(N, out\_channels)`. The data type is float32.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `in_channels` or `out_channels` is not an int.</span>
<span class="sd">        TypeError: If `has_bias`, `has_bn` or `after_fake` is not a bool.</span>
<span class="sd">        TypeError: If `momentum` or `eps` is not a float.</span>
<span class="sd">        ValueError: If `momentum` is not in range [0, 1.0].</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; net = nn.DenseBnAct(3, 4)</span>
<span class="sd">        &gt;&gt;&gt; x = Tensor(np.random.randint(0, 255, [2, 3]), mindspore.float32)</span>
<span class="sd">        &gt;&gt;&gt; result = net(x)</span>
<span class="sd">        &gt;&gt;&gt; output = result.shape</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        (2, 4)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">in_channels</span><span class="p">,</span>
                 <span class="n">out_channels</span><span class="p">,</span>
                 <span class="n">weight_init</span><span class="o">=</span><span class="s1">&#39;normal&#39;</span><span class="p">,</span>
                 <span class="n">bias_init</span><span class="o">=</span><span class="s1">&#39;zeros&#39;</span><span class="p">,</span>
                 <span class="n">has_bias</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                 <span class="n">has_bn</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">momentum</span><span class="o">=</span><span class="mf">0.9</span><span class="p">,</span>
                 <span class="n">eps</span><span class="o">=</span><span class="mf">1e-5</span><span class="p">,</span>
                 <span class="n">activation</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">alpha</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span>
                 <span class="n">after_fake</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize DenseBnAct.&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">DenseBnAct</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dense</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span>
            <span class="n">in_channels</span><span class="p">,</span>
            <span class="n">out_channels</span><span class="p">,</span>
            <span class="n">weight_init</span><span class="p">,</span>
            <span class="n">bias_init</span><span class="p">,</span>
            <span class="n">has_bias</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">has_bn</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">has_bn</span><span class="p">,</span> <span class="s2">&quot;has_bn&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">has_act</span> <span class="o">=</span> <span class="n">activation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">after_fake</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">after_fake</span><span class="p">,</span> <span class="s2">&quot;after_fake&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">has_bn</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">batchnorm</span> <span class="o">=</span> <span class="n">BatchNorm1d</span><span class="p">(</span><span class="n">out_channels</span><span class="p">,</span> <span class="n">eps</span><span class="p">,</span> <span class="n">momentum</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">activation</span> <span class="o">==</span> <span class="s2">&quot;leakyrelu&quot;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">activation</span> <span class="o">=</span> <span class="n">LeakyReLU</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">activation</span> <span class="o">=</span> <span class="n">get_activation</span><span class="p">(</span><span class="n">activation</span><span class="p">)</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">activation</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="k">else</span> <span class="n">activation</span>
            <span class="k">if</span> <span class="n">activation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">,</span> <span class="p">(</span><span class="n">Cell</span><span class="p">,</span> <span class="n">Primitive</span><span class="p">)):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For &#39;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="si">}</span><span class="s2">&#39;, the &#39;activation&#39; must be str or Cell or Primitive, &quot;</span>
                                <span class="sa">f</span><span class="s2">&quot;but got </span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="n">activation</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2">.&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dense</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_bn</span><span class="p">:</span>
            <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">batchnorm</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_act</span><span class="p">:</span>
            <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">x</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>